<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">
        搜索
      </el-button>
      <el-button v-waves v-permission="['/article/questionEdit']" class="filter-item" type="primary" @click="add()">
        <i class="el-icon-plus" />&nbsp;&nbsp;添加文章
      </el-button>
    </div>



    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="70">
        <template slot-scope="scope">
          <span>{{ scope.row.Id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.Question }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回答" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.Answer }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="文章排序" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.Sort }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="提问时间" align="center" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.AskTime }}</span>
        </template>
      </el-table-column>

      <el-table-column v-permission="['/article/questionGet','/article/questionEdit', '/article/questionDel']" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <div>
            <span v-permission="['/article/questionEdit']">
              <el-button size="small" type="primary" @click="edit(scope.row.Id)">
                编辑
              </el-button>
            </span>
            <span v-permission="['/article/questionDel']">
              <el-button size="small" type="danger" @click="del(scope.row)">
                删除
              </el-button>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog title="答疑详情" :visible.sync="showArticleVisible">
      <span v-if="showList.AskTime">提问时间:{{ showList.AskTime}}</span> &nbsp;&nbsp;&nbsp;<span v-if="showList.AnswerTime">回答时间:{{ showList.AnswerTime}}</span>
<!--      <el-row v-if="showList.edit">
        <el-col :span="24">
          <div class="grid-content bg-purple-dark">
            <h3 style="text-align:center">{{ showList.Question }}</h3>
          </div>
        </el-col>
      </el-row> -->
      <el-form>
        <el-form-item label="问题" label-width="70px">
          <el-input v-model="showList.Question" type="textarea" :rows="4"/>
        </el-form-item>
      </el-form>
      <el-form>
        <el-form-item label="回答" label-width="70px">
          <el-input v-model="showList.Answer" type="textarea" :rows="4"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { questionGet,questionEdit,questionDel, questionList } from '@/api/article'
import waves from '@/directive/waves'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import permission from '@/directive/permission/index.js'

export default {
  components: { Pagination },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        keywords:'',
      },

      showArticleVisible: false,
      showList: {
        Question:null,
        Answer:null,
      },

      catelist:null,

      questionEditContent:{

      },
      questionEditVisible:false,

    }
  },
  created() {
    this.getList()
  },

  methods: {
    getList() {
      this.listLoading = true
      questionList(this.listQuery).then(res => {
        this.list = res.data.data
        this.total = res.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 0.3 * 1000)
      })
    },

    add() {
      this.showArticleVisible = true
      this.showList = {
        Question:null,
        Answer:null,
        AskTime:false,
        AnswerTime:false
      }
    },
    handleEdit(){
      questionEdit(this.showList).then(res => {
        if (res.code == 20000) {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.showArticleVisible = false
          this.getList()
        }
      })
    },

    edit(row) {
      this.showArticleVisible = true
      questionGet({id:row}).then(res => {
        if (res.code == 20000) {
          console.log(res.data);
          this.showList = res.data
        } else {
          this.showList.Ask = ''
          this.showList.Content = ''
        }
      })
    },
    // 删除答疑
    del(row) {
      this.$confirm('确认删除该问题?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        questionDel({Id:row.Id}).then(res => {
          this.getList()
          this.$message({
            message: '该问题已删除',
            type: 'success'
          })
        })
      }).catch(() => {
      })
    },

  }
}
</script>
